package com.zhu.service;

import com.zhu.mapper.*;
import com.zhu.pojo.Expend;
import com.zhu.pojo.Income;
import com.zhu.pojo.TransSum;
import com.zhu.pojo.Transform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IncomeService {
    @Autowired
    IncomeMapper incomeMapper;
    @Autowired
    WayMapper wayMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    UserMapper userMapper;


    public List<Transform> getAll(String username) {
        int userId= userMapper.getUserByName(username).getId(); //获取用户id
        List<Income> incomeList = incomeMapper.getAll(userId);//查询此用户的所有信息
        ArrayList<Transform> list = new ArrayList<>();

        for (Income income : incomeList) {
            Transform transform = new Transform();
            transform.setId(income.getIid());
            transform.setTime(income.getTime());
            transform.setTid(income.getTypeId());
            transform.setWid(income.getWayId());
            if(typeMapper.getById(income.getTypeId()) != null){
                transform.setTypeName(typeMapper.getById(income.getTypeId()).getName());//通过typeid查询，并赋值transfrom.typeName
            } else {
                transform.setTypeName("");
            }
            if(wayMapper.getById(income.getWayId()) != null) {
                transform.setWayName(wayMapper.getById(income.getWayId()).getName());//通过wayid查询，并赋值transfrom.wayName
            } else {
                transform.setWayName("");
            }
            transform.setMoney(income.getIncomeMoney());
            list.add(transform);
        }
        return list;
    }

    public boolean save(Income income) {
        int row = incomeMapper.add(income);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public boolean upd(Income income) {
        int row = incomeMapper.update(income);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public List<Transform> getByCondition(Income co) {

        List<Income> incomeListList = incomeMapper.getByCondition(co); //查询此用户的所有信息

        ArrayList<Transform> list = new ArrayList<>();

        for (Income income : incomeListList) {
            Transform transform = new Transform();
            transform.setId(income.getIid());
            transform.setTime(income.getTime());
            transform.setTid(income.getTypeId());
            transform.setWid(income.getWayId());
            if(typeMapper.getById(income.getTypeId()) != null){
                transform.setTypeName(typeMapper.getById(income.getTypeId()).getName());//通过typeid查询，并赋值transfrom.typeName
            } else {
                transform.setTypeName("");
            }
            if(wayMapper.getById(income.getWayId()) != null) {
                transform.setWayName(wayMapper.getById(income.getWayId()).getName());//通过wayid查询，并赋值transfrom.wayName
            } else {
                transform.setWayName("");
            }
            transform.setMoney(income.getIncomeMoney());
            list.add(transform);
        }
        return list;
    }

    public List<Map<Object,Object>> getNumAndName(TransSum tr) {
        return incomeMapper.getNumAndName(tr);
    }

    public boolean del(int  id) {
        int row = incomeMapper.delete(id);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public TransSum summation(TransSum tr){
        return  incomeMapper.getSum(tr);
    }


    public List<Income> getAllByAdmin() {
        return incomeMapper.getAllByAdmin();
    }

    public List<Income> getByAdmin(Income income) {
        return incomeMapper.getByAdmin(income);
    }
}

